<template>
  <div>
    <el-table
      :default-sort="{ prop: 'date', order: 'descending' }"
      :data="tableData"
      :style="{ width: '97%', }"
      border='1'

    >
      <el-table-column fixed prop="id" label="ID" width="50px" />
      <el-table-column prop="name" label="车长" />
      <el-table-column prop="train" label="车次" width="100px" />
      <el-table-column prop="type" label="车次类型" width='100px' />
      <el-table-column label="线路" width="160px">
        <template #default="scope">
          {{ scope.row.line }} <el-icon><Right /></el-icon>
          {{ scope.row.line1 }}
        </template>
      </el-table-column>
      <el-table-column prop="date" label="发车日期" width="130px" sortable />
      <el-table-column prop="con" label="车厢总数" />
      <el-table-column prop="seat" label="总座位数" />
      <el-table-column prop="limited" label="限乘人数" />
      <el-table-column  label="状态"  >
        <template #default="scope">
          <el-switch
            :model-value="scope.row.state === '启用' ? true : false"
            class="ml-2"
            inline-prompt
            active-icon="Watermelon"
            inactive-icon="Sugar"
            @change="
              () => {
                SetState(scope.row.id);
              }
            "
            :style="{
              marginLeft: '24px',
              '--el-switch-on-color': ' #13ce66',
              '--el-switch-off-color': '#F56C6C',
            }"
          />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150px">
        <template #default="scope">
          <el-popconfirm
            width="200"
            @confirm="() => del(scope.row.id)"
            :title="'确定删除线路' + scope.row.line + '?'"
          >
            <template #reference>
              <el-button type="danger" size="small">删除</el-button>
            </template>
          </el-popconfirm>
          <el-button size="small" type="success" @click="edit(scope.row)"
            >编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 路线 编辑 -->
    <el-dialog
      v-model="show"
      title="线路 编辑管理"
      width="90%"
      :before-close="handleClose"
    >
      <!-- form ...... -->
      <el-form :model="form" status-icon label-width="120px" class="form">
        <el-form-item label="车长">
          <el-input v-model="form.name" placeholder="车长" /> </el-form-item
        ><el-form-item label="车次">
          <el-input v-model="form.train" placeholder="车次" />
        </el-form-item>
        <el-form-item label="线路1">
          <el-input v-model="form.line" placeholder="线路1" /> </el-form-item
        ><el-form-item label="线路2">
          <el-input v-model="form.line1" placeholder="线路2" />
        </el-form-item>
        <el-form-item label="车厢">
          <el-input-number v-model="form.con" :min="1" :max="13" />
        </el-form-item>
        <el-form-item label="类型">
          <el-select
            v-model="form.type"
            class="m-2"
            placeholder="Select"
            size="default"
            @change="
              () => {
                this.$emit('getlist');
              }
            "
          >
            <el-option
              v-for="item in options"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
          <el-form-item label="状态">
            <el-select
              v-model="form.state"
              class="m-2"
              placeholder="Select"
              size="default"
              @change="
                () => {
                  this.$emit('getlist');
                }
              "
            >
              <el-option
                v-for="item in options1"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="form.date"
            type="date"
            placeholder="请输入查询时间"
            value-format="YYYY-MM-DD"
            :default-value="new Date(2022, 8, 27)"
            @change="$emit('getlist')"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="EditSub()">修改</el-button>
          <el-button @click="() => (show = false)"> 取消 </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { HttpState, HttpDel, EditLine } from "api";
import { ElMessage } from "element-plus";

export default {
  data() {
    return {
      show: false,
      tableData: [],
      form: {
        line: "",
        line1: "",
        time: "",
        time1: "",
        remain: "",
        date: "",
        business: "",
        state: "",
        sub: "",
        type: "",
      },
      options: [
        "高速动车",
        "动车组",
        "城际高速",
        "直达特快",
        "空调特快",
        "快速",
        "普快",
      ],
      options1: ["已提交", "未提交"],
    };
  },
  props: {
    tableData: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  methods: {
    async del(id) {
      const res = await HttpDel({ id });
      ElMessage({
        message: res.data.msg,
      });
      this.$emit("getlist");
    },
    async SetState(id) {
      const res = await HttpState({ id });
      ElMessage({
        type: "warning",
        message: res.data.msg,
      });
      this.$emit("getlist");
    },
    async edit(row) {
      this.show = true;
      this.form = { ...row };
      ElMessage({
        message: res.data.msg,
      });
      this.$emit("getlist");
    },
    async EditSub() {
      const res = await EditLine({ ...this.form });
      console.log(res.data);
      ElMessage({
        message: res.data.msg,
        type: "warrning",
      });
      this.show = false;
      this.$emit("getlist");
    },
  },
};
</script>
 